package com.aek.aekdroid;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import com.aek.aekdroid.data.Match;

public class WebAekDroid {
	// public static final String DOMAIN =
	// "http://192.168.0.100/webaekdroid/local.php";
	public static final String DOMAIN = "http://www.aek.com/webaekdroid/index.php";

	public static final String TAG = "WebAekDroid";
	public static final String UPDATE_URL = DOMAIN + "?r=match/list";
	public static final String VERSION_URL = DOMAIN + "?r=settings/value&name=Version";
	public static final String CREATE_URL = DOMAIN + "?r=match/createFromMobile";
	public static final String ADD_DETAIL_URL = DOMAIN + "?r=detail/createFromMobile";
	public static final String ADD_ELEVEN_URL = DOMAIN + "?r=match/elevenFromMobile";
	public final static String ADMIN_URL = DOMAIN + "?r=settings/check";
	public final static String DELETE_MATCH = DOMAIN + "?r=match/deleteFromMobile";
	public final static String DELETE_DETAIL = DOMAIN + "?r=detail/deleteFromMobile";
	public boolean isConnected = false;
	HttpURLConnection httpConn;
	private AekDroidApplication aek;

	public WebAekDroid(AekDroidApplication aek, String url) {
		this.aek = aek;
		isConnected = connect(url);
		MyLog.d(TAG, "onCreate");
	}

	private boolean connect(String connUrl) {
		MyLog.d(TAG, "Connecting to " + connUrl);
		try {
			URL url = new URL(connUrl);
			String host = aek.getPrefs().getString(aek.getString(R.string.pref_proxyHostKey), "");
			String port = aek.getPrefs().getString(aek.getString(R.string.pref_proxyPortKey), "");
			if (!host.equals("") && !port.equals("")) {
				System.setProperty("http.proxyHost", host);
				System.setProperty("http.proxyPort", port);
				MyLog.d(TAG, "Connecting with " + host + ":" + port);
			}
			URLConnection conn = url.openConnection();
			httpConn = (HttpURLConnection) conn;
			// httpConn.setAllowUserInteraction(false);
			// httpConn.setInstanceFollowRedirects(true);
			// httpConn.setRequestMethod("GET");
			httpConn.connect();
			return true;
		} catch (MalformedURLException e) {
			MyLog.e(TAG, "Malformed url");
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			MyLog.e(TAG, "Could not read from the url");
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			MyLog.e(TAG, "Unknown exception while connecting to the url");
			e.printStackTrace();
			return false;
		}
	}

	public String getNewVersion() throws IOException {
		int response = -1;
		response = httpConn.getResponseCode();

		if (response == HttpURLConnection.HTTP_OK) {
			// in = httpConn.getInputStream();
			String url = VERSION_URL + "&name=Version";
			MyLog.d(TAG, "url : " + url);
			URL u = new URL(url);
			URLConnection urlConnection = u.openConnection();
			BufferedInputStream in = new BufferedInputStream(urlConnection.getInputStream());
			String line = "";
			int theChar;
			while ((theChar = in.read()) != -1) {
				line += (char) theChar;
			}
			in.close();
			MyLog.d(TAG, "New version is " + line);
			return line;
		}
		return null;
	}

	public List<Match> getMatches(long lastTimeUpdated, String period) throws IOException {
		int response = -1;
		response = httpConn.getResponseCode();

		if (response == HttpURLConnection.HTTP_OK) {
			// in = httpConn.getInputStream();
			String url = UPDATE_URL + "&updated=" + lastTimeUpdated + "&period=" + period;
			MyLog.d(TAG, "url : " + url);
			MatchReader mr = new MatchReader(url);
			mr.read();
			return mr.matches;
		}
		return null;
	}

}
